package com.es.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * 描述:
 * MyBatis-
 *
 * @author Derek
 * 创建时间：2022-05-09 9:37
 */
public class MybatisUtils {
    private static final String CONFIG_FILE_NAME  = "mybatis-config.xml";
    private static SqlSessionFactory factory = null;
    static{
        //
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        try {
            Reader reader = Resources.getResourceAsReader(CONFIG_FILE_NAME);
            factory = builder.build(reader);
        } catch (IOException e) {
            System.out.println("filename：mybatis-config.xml");
            e.printStackTrace();
        }
    }
    //
    public static SqlSession getSqlSession(){
        return getSqlSession(true);
    }
    public static SqlSession getSqlSession(boolean autoCommit){
        return factory.openSession(autoCommit);
    }
}
